/* api */
import * as FieldApi from '@src/api/FieldApi'
/* service */
import { isLogisticsNoField } from '@service/FieldService'

const globalMixin = {
  name: 'logistics-mixin',
  data() {
    return {
      //
    }
  },
  mounted() {
  },
  methods: {
    /* 查询是否还有剩余可使用次数 */
    async checkQuota() {
      
      try {
        
        this.hasQuota = await FieldApi.getLogisticsQuotaDetail();
        
        return this.hasQuota;
        
      } catch (err) {
        
        const message = (err && err.message) || t('common.base.tip.noQueryTimes');
        
        this.$alert(message);
      }
      
    },
    async handlerClickViewLogisticsForList(row, column, mode, bizId) {

      // 是否为物流单号字段
      const isLogisticsNo = this.isLogisticsNo(column)
      const fieldName = column.fieldName
      const splitFieldNames = fieldName.split('_')
      const rawFieldName = `${splitFieldNames[0]}_${splitFieldNames[1]}`
      const value = row?.attribute?.[rawFieldName]
      
      /** 
       * 1. 不是物流单号字段
       * 2. 并且没有物流记录
      */
      if (!isLogisticsNo || !value || !value.length) return
      
      const { company, no, phone } = value[0]
      
      /** 
       * 没有物流单号
      */
      if(!no || !column.setting.canCheckForStaff) return
      
      const has = await this.checkQuota()
      // 没有剩余查询次数
      if (!has) return
      
      const logisticsInfo = { ...company, no, phone };
      
      // 打开物流信息弹窗
      this.$fast.biz.initLogisticsNoDialog({
        visible: true,
        info: logisticsInfo,
        mode,
        bizId,
      })
      
    },
    async handlerClickViewLogisticsForDetail() {
      // 
    },
    /**
     * @description 是否是物流单号字段
    */
    isLogisticsNo(field) {
      return isLogisticsNoField(field);
    },
    /** 
     * @description 是否可以点击查看物流信息
    */
    isCanLogisticsNoLink(column) {
      return Boolean(this.hasQuota && this.isLogisticsNo(column) && column?.setting?.canCheckForStaff)
    }
  }
}

export default globalMixin
